//
//  LoginViewController.m
//  OneDoller
//
//  Created by jyj on 16/8/12.
//  Copyright © 2016年 jyj. All rights reserved.
//

#import "LoginViewController.h"
#import <AFNetworking.h>
#import <CommonCrypto/CommonCryptor.h>
#import <CommonCrypto/CommonDigest.h>


@interface LoginViewController ()
@property(nonatomic,strong) AFHTTPSessionManager *session;
@property(nonatomic,strong) NSString *time;
@property(nonatomic,strong) NSString *token;
@property (weak, nonatomic) IBOutlet UIButton *LoginBtn;//登录按钮
@property (weak, nonatomic) IBOutlet UILabel *MobileID;//手机账户
@property (weak, nonatomic) IBOutlet UILabel *LogPwd;//登录密码
@property (weak, nonatomic) IBOutlet UITextField *MobileIDTextField;
@property (weak, nonatomic) IBOutlet UITextField *LoginPwdField;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatRound];
    [self getCurTimeAndToken];
    self.navigationController.navigationBar.barTintColor = [UIColor orangeColor];
    self.session = [ AFHTTPSessionManager manager];//初始化对象
    self.session.requestSerializer = [AFHTTPRequestSerializer serializer];//申明请求的数据类型
    self.session.responseSerializer = [AFHTTPResponseSerializer serializer];//申明返回的数据类型
    self.session.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"application/x-json",@"text/html", nil];//申明返回的数据类型可以接收哪些格式
}
/**
 *  创建圆角
 */
-(void)creatRound
{
    self.LoginBtn.layer.masksToBounds = YES;
    self.LoginBtn.layer.cornerRadius = 6.0;
    self.LoginBtn.layer.borderWidth = 1.0;
    self.LoginBtn.layer.borderColor = [[UIColor whiteColor] CGColor];

}
/**
 * 获取当前时间戳
 * 获取token
 */
-(void)getCurTimeAndToken
{
    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval a = [dat timeIntervalSince1970];
    NSString *timeString = [NSString stringWithFormat:@"%.0f", a];
    NSString *token = @"jtOneDollar";
    self.time = timeString;
    self.token = token;
    
}
- (IBAction)LoginBtnPressed:(id)sender {
    /*将token参数和时间戳加入数组并进行升序排序*/
    NSArray *arr = @[self.token,self.time];
    NSSortDescriptor *sd = [NSSortDescriptor sortDescriptorWithKey:nil ascending:YES];
    NSArray *myary = [arr sortedArrayUsingDescriptors:[NSArray arrayWithObjects:sd, nil]];
    NSString *strings = [myary componentsJoinedByString:@""];
    NSString *str1= [self sha1:strings];
    
    /*AFNetWorking库请求方式*/
     NSString * url = @"http://192.168.1.107:8080/OneDollar/jt/login.do";//URL参数
     NSMutableDictionary *dict = [NSMutableDictionary dictionary];
     [dict setObject:self.MobileIDTextField.text forKey:@"loginname"];//长传的参数
     [dict setObject:self.LoginPwdField.text forKey:@"password"];
     [dict setObject:@"0" forKey:@"logintype"];
     [dict setObject:str1 forKey:@"signature"];
     [dict setObject:self.time forKey:@"timestamp"];
  
     [self.session POST:url parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) {
         NSLog(@"上传的进度");
     } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
         NSString *str=[[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
         NSLog(@"str = %@",str);
       

     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
         NSLog(@"post请求失败:%@", error);
     }];
}
                            
/*
 *SHA1加密
 */
- (NSString *)sha1:(NSString *)input
{
        const char *cstr = [input cStringUsingEncoding:NSUTF8StringEncoding];
        NSData *data = [NSData dataWithBytes:cstr length:input.length];
        
        uint8_t digest[CC_SHA1_DIGEST_LENGTH];
        
        CC_SHA1(data.bytes, data.length, digest);
        
        NSMutableString *output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
        
        for(int i=0; i<CC_SHA1_DIGEST_LENGTH; i++) {
            [output appendFormat:@"%02x", digest[i]];
        }
    
    return output;

}
@end


